#! /bin/bash

set -e

# get the directory of the script
function get_script_dir() {
    local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
    echo "${script_dir}"
}
THIS_DIR=$(realpath "$(get_script_dir)")
source ${THIS_DIR}/envcheck.sh

pushd ${APOLLO_ROOT_DIR}/
    NEOLIX_RELATIVE_SCRIPT_DIR=$(realpath --relative-to=${APOLLO_ROOT_DIR} ${NEOLIX_INSTALL_SCRIPT_DIR}/onboard_files)
    ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/usr usr
    ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/../../docker_scripts/orin_into.sh orin_into.sh 
    pushd cyber
        cp ${NEOLIX_RELATIVE_SCRIPT_DIR}/cyber/transport/shm/condition_notifier.cc ${APOLLO_SCRIPTS_DIR}/cyber/transport/shm/condition_notifier.cc
    popd
    pushd modules
        pushd calibration/data
            for vehicle_dir in ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/calibration/data/*/ ; do
                [ -d "$vehicle_dir" ] || continue
                ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/calibration/data/${vehicle_dir} ${vehicle_dir}
            done
        popd
        pushd canbus_vehicle
            cp ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/canbus_vehicle/BUILD BUILD
            ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/canbus_vehicle/x3epro x3epro
        popd
        pushd common
            cp ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/common/data/global_flagfile.txt global_flagfile.txt
        popd
        pushd dreamview/conf
            cp ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/dreamview/conf/vehicle_data.pb.txt vehicle_data.pb.txt
            pushd hmi_modes
                for mode_file in ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/dreamview/conf/hmi_modes/* ; do
                    [ -f "$mode_file" ] || continue 
                    ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/dreamview/conf/hmi_modes/${mode_file} ${mode_file}
                done
            popd
        popd
        pushd monitor/dag
            cp ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/monitor/dag/monitor.dag monitor.dag
        popd
        pushd drivers
            ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/drivers/orin_camera orin_camera
            ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/drivers/orin_gnss_v2 orin_gnss_v2
            pushd lidar
                ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/drivers/lidar/orin_hesai orin_hesai
                ln -s ${NEOLIX_RELATIVE_SCRIPT_DIR}/modules/drivers/lidar/livox livox
            popd
        popd
    popd
popd

echo "done"